package br.com.loja.ManagedBean;

import java.sql.SQLException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import br.com.loja.Bean.CadastroBean;
import br.com.loja.Bean.LoginBean;
import br.com.loja.Dao.CadastroDao;
import br.com.loja.Dao.LoginDao;


@ManagedBean(name = "cadastroBean")
@SessionScoped
public class CadastroManagedBean {
	CadastroBean cadastro = new CadastroBean();
	LoginBean login = new LoginBean();
	boolean readOnly;
	List<CadastroBean> lista;
	
	public String inserirCliente() throws SQLException {
		CadastroDao dao = new CadastroDao();
		dao.inserirClietne(cadastro);
		
		return "../Clientes/sucesso.xhtml?faces-redirect=true";

	}

	public String login() throws SQLException {
		LoginDao dao = new LoginDao();
		dao.inserirLogin(login);
		
		return "../Login/sucesso.xhtml?faces-redirect=true";
		
	}
	
	public String confirmaLogin() throws SQLException{
		LoginDao dao = new LoginDao();
		List<LoginBean> lista = dao.verificarLogin();
		
			for(LoginBean lista1:lista){
				if(!lista.isEmpty()){
					if(login.getUsuario().equals(lista1.getUsuario()) && login.getSenha().equals(lista1.getSenha())){
						return "../index.xhtml?faces-redirect=true";
					}
				}
				
			}
			
		return "erro.xhtml";
	}
	
	public List<CadastroBean> lista() throws SQLException{
		
		CadastroDao dao = new CadastroDao();
		lista = dao.lista();
		
		
		return lista;
		
	}
	
	public String voltar(){
		return "../index.xhtml?faces-redirect=true";
		
	}
	public String sair(){
		return "../index.xhtml?faces-redirect=true";
	}
	
	public String voltarLogin(){
		return "../Login/login.xhtml?faces-redirect=true";
	}
	
	public String sairLogin(){
		return "../Login/login.xhtml?faces-redirect=true";
	}
	public CadastroBean getCadastro() {
		return cadastro;
	}

	public void setCadastro(CadastroBean cadastro) {
		this.cadastro = cadastro;
	}

	public LoginBean getLogin() {
		return login;
	}

	public void setLogin(LoginBean login) {
		this.login = login;
	}

	public boolean isReadOnly() {
		return readOnly;
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}

	public List<CadastroBean> getLista() {
		return lista;
	}

	public void setLista(List<CadastroBean> lista) {
		this.lista = lista;
	}

	
}
